Function LuaToVector3F(l:Lua_State; i:integer):TVector3F; cdecl;
begin
luaCheckTableType(l, i, 'LuaToVector');
lua_RawGetI(l, i, 1);
result[0]:=lua_tonumber(l, -1);
lua_pop(l, 1);
                     lua_RawGetI(l, i, 2);
result[1]:=lua_tonumber(l, -1);
lua_pop(l, 1);
                                                               lua_RawGetI(l, i, 3);
result[2]:=lua_tonumber(l, -1);
lua_pop(l, 1);
end;
procedure luaPushVector3F(l:lua_state; v:TVector3F); cdecl;
var i:integer;
begin
lua_createtable(l, 3,0);
              for i:=0 to 2 do begin
              lua_pushNumber(l, v[i]);
              lua_rawsetI(l, -2, i+1);
end;
end;

function luaUses(l:lua_State):integer; cdecl;
var s:string;
begin
             s:=lual_checkstring(l,1);
lua_getfield(l, lua_globalsindex, 'package');
lua_getfield(l, -1, 'loaded');
lua_remove(l,-2);
lua_getfield(l, -1, pchar(s));
if lua_toboolean(l, -1)=true then begin
lua_remove(l,-2);
result:=1;
exit;
end;
lua_pop(l,1);
{if Resource^.GetByName(PChar(s))=nil then
Resource^.load(PChar(s), rt_script);}
///lual_error(l, pchar('uses(): '+e.message),[]);
ExecResource(PChar(s));
lua_getfield(l, lua_globalsindex, 'package');
lua_getfield(l, -1, 'loaded');
lua_remove(l,-2);
lua_getfield(l, -1, pchar(s));
lua_remove(l,-2);
result:=1;
end;

Function LuaExecute(l:Lua_state):integer; cdecl;
begin
if lua_Gettop(l) <> 1 then lual_error(l, 'execute expects 1 parameter (resourceName).', []);
{if Resource^.getByName(lua_tostring(l, 1)) = nil then
resource^.load(lua_tostring(l, 1), rt_Script);}
ExecResource(lua_tostring(l, 1));
result:=0;
end;

Function LuaLog(l:lua_state):integer; cdecl;
var i: integer;
s:string;
begin
if lua_gettop(l) <2 then lual_error(l, 'log expects 2 parameters',[]) else begin
for i := 1 to lua_gettop(l) -1 do
s := s + luatostring(l,i);
log_write(PChar(s), TLogLevel(lua_tointeger(l,-1)));
end;
result:=0;
end;

function luares_LoadPackage(L: lua_state): Integer; cdecl;
begin
  if lua_gettop(L)<1 then
    luaL_error(L, 'res.loadPackage expects 1 parameter (fileName).',[])//123
  else
    begin
resourceManager^.LoadPackage(Lua_tostring(L, 1));
    end;
  result := 0
end;

    Function LuaEngineQuit(l:lua_state):integer; cdecl;
begin
if lua_gettop(l) <> 0 then lual_error(l, 'engine.quit expects no parameters.', []);
core_setState(core_terminating);
result:=0;
end;
    Function LuaEngineOpenConsole(l:lua_state):integer; cdecl;
    begin
if lua_gettop(l) <>0 then lual_error(l, 'engine.openConsole expects no parameters.', []);
IConsole(core_requirePlugin(ConsoleI.ConsoleGUID))^.Open();
result:=0;
    end;

    {$i LuaBindings_SoundSystem.inc}
{$i LuaBindings_Speech.inc}
    Procedure RegisterBindings;
begin
if core_PluginExists(SoundSystemi.SoundSystemGUID) then RegisterSoundSystem;
if core_PluginExists(SoundSystemi.SoundGeometryGUID) then RegisterSoundGeometry;
if core_PluginExists(SoundSystemi.ReverbGUID) then RegisterReverb;
if Core_PluginExists(SpeechI.SpeechGUID) then RegisterSpeech;
luaRegister(l, 'log', luaLog);
lua_pushinteger(l, cardinal(ll_critical));
lua_setglobal(l, 'll_critical');
lua_pushinteger(l, cardinal(ll_error));
lua_setglobal(l, 'll_error');
lua_pushinteger(l, cardinal(ll_warn));
lua_setglobal(l, 'll_warn');
lua_pushinteger(l, cardinal(ll_info));
lua_setglobal(l, 'll_info');
lua_pushinteger(l, cardinal(ll_debug));
lua_setglobal(l, 'll_debug');
lua_pushinteger(l, rt_unknown);
lua_setglobal(l, 'rt_unknown');
lua_pushinteger(l, rt_3DSound);
lua_setglobal(l, 'rt_3DSound');
lua_pushinteger(l, rt_script);
lua_setglobal(l, 'rt_script');
                              lua_pushinteger(l, rt_2DSound);
                               lua_setglobal(l, 'rt_2DSound');
{                               lua_pushinteger(l, rt_2DSoundStreamed);
lua_setglobal(l, 'rt_2DSoundStreamed');}
                               luaRegister(l, 'res.loadPackage', luaRes_LoadPackage);
LuaRegister(l, 'uses', LuaUses);
LuaRegister(l, 'execute', LuaExecute);
luaRegister(l, 'engine.quit', LuaEngineQuit);
LuaRegister(l, 'engine.openConsole', LuaEngineOpenConsole);
end;
